package com.knife.review;

import java.sql.*;
import java.util.*;

import com.knife.db.ComDAL;
import com.knife.tools.*;
import org.apache.log4j.*;
import com.knife.tools.Util;

public class BlogMessage {

    private String cid;
    private String parentId;
    private String belonguser;
    private String title;
    private String content;
    private String ip;
    private java.util.Date inputtime;
    private String inputuser;
    private String url;
    private Integer status;

    public String getBelongUser() {
        return belonguser;
    }

    public void setBelongUser(String belonguser) {
        this.belonguser = belonguser;
    }

    public String getCid() {
        return cid;
    }

    public void setCid(String cid) {
        this.cid = cid;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public java.util.Date getInputTime() {
        return inputtime;
    }

    public void setInputTime(java.util.Date inputtime) {
        this.inputtime = inputtime;
    }

    public String getInputUser() {
        return inputuser;
    }

    public void setInputUser(String inputuser) {
        this.inputuser = inputuser;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Vector<BlogMessage> getListAll(String Env, String orderstr, int PageSize,
            int page) {
        if (page <= 0) {
            page = 1;
        }
        Vector<BlogMessage> vc = new Vector<BlogMessage>();
        String strSql = " select * from blogmessage where 1=1";
        if (!Env.equals("")) {
            strSql = strSql + " and " + Env;
        }
        if (orderstr.equals("")) {
        } else {
            strSql = strSql + " order by " + orderstr;
        }
        strSql = strSql + " limit " + (page - 1) * PageSize + "," + PageSize;
        ComDAL theDAL = null;
        try {
            theDAL = new ComDAL();
            System.out.println(strSql);
            // if(true)return null;
            ResultSet rs = theDAL.DoSelect(strSql);
            if (rs != null) {
                do {
                    BlogMessage review = new BlogMessage();

                    review.belonguser = rs.getString("belonguser");
                    review.cid = rs.getString("cid");
                    review.parentId = rs.getString("parentid");

                    review.title = rs.getString("title");
                    review.content = rs.getString("content");
                    review.ip = rs.getString("ip");
                    review.inputtime = rs.getDate("inputtime");
                    review.inputuser = rs.getString("inputuser");
                    review.url = rs.getString("url");
                    review.status = rs.getInt("status");
                    vc.addElement(review);
                } while (rs.next());
                rs.close();
            }
        } catch (Exception e) {
            theDAL.close();
            e.printStackTrace();
        }
        theDAL.close();
        return (vc);
    }

    public BlogMessage getObjectByID(String cid) {

        if (getListAll(" cid='" + cid + "'", "", 1, 1).size() == 1) {
            return getListAll(" cid='" + cid + "'", "", 1, 1).get(0);
        } else {

            return null;
        }

    }

    public int getCount(String env) {
        String sqlstr = "select count(*) as results from blogmessage where 1=1";
        if (!env.equals("")) {
            sqlstr += " and " + env;
        }
        ComDAL theDAL = null;
        try {
            theDAL = new ComDAL();
            ResultSet rs = theDAL.DoSelect(sqlstr);
            if (rs != null) {
                return rs.getInt("results");
            } else {
                return 0;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return 0;
        }
    }

    public int updateObject(BlogMessage review) {
        String sqlstr = "update blogmessage set belonguser='" + review.belonguser + "',status=" + review.status + " where cid=" + review.cid;
        ComDAL theDAL = null;
        try {
            theDAL = new ComDAL();
            int result_count = theDAL.execCommand(sqlstr);
            return result_count;
        } catch (Exception e) {
            return -1;
        }

    }
}
